//
//  MainViewController.m
//  coca
//
//  Created by 张帅 on 15/9/26.
//  Copyright © 2015年 ZhangShuai. All rights reserved.
//

#import "MainViewController.h"
#import "DiscoverTableViewController.h"
#import "MessageTableViewController.h"
#import "HomeTableViewController.h"
#import "ProfileTableViewController.h"
#import "ComposeViewController.h"
#import "UserAccountViewModel.h"
#import "OAuthViewController.h"
@interface MainViewController ()

@property(nonatomic,strong)UIButton *composedButton;

@end

@implementation MainViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addChildViewControllers];
 
  
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
       [self setupComposedButton];
}

- (void)addChildViewControllers {
    
    self.tabBar.tintColor = [UIColor orangeColor];
    
    HomeTableViewController *homevc =[[HomeTableViewController alloc] init];
    DiscoverTableViewController *discoverVC =[[DiscoverTableViewController alloc] init];
    MessageTableViewController *messageVc = [[MessageTableViewController alloc] init];
    ProfileTableViewController *proFileVc =[[ProfileTableViewController alloc] init];
    

    [self addchildVC:homevc title:@"首页" imageName:@"tabbar_home"];
    
    [self addchildVC:messageVc title:@"消息" imageName:@"tabbar_message_center"];
    
    UITableViewController * tableViewVC =[[UITableViewController alloc] init];
    
    [self addChildViewController:tableViewVC];
    
    [self addchildVC:discoverVC title:@"发现" imageName:@"tabbar_discover"];
    [self addchildVC:proFileVc title:@"我" imageName:@"tabbar_profile"];
    

}



- (void) addchildVC:(UIViewController*)viewVC title:(NSString*)title imageName:(NSString*)imageName{
    
    viewVC.title = title;
    
    viewVC.tabBarItem.image = [UIImage imageNamed:imageName] ;
    
    UINavigationController * navc =[[ UINavigationController alloc]initWithRootViewController:viewVC];
    navc.navigationBar.tintColor =[UIColor orangeColor];
    [self addChildViewController:navc];
}

- (void)setupComposedButton{

    
    NSInteger count = self.childViewControllers.count;
    
    CGFloat  w = self.tabBar.bounds.size.width /count;
    CGFloat h = self.tabBar.bounds.size.height;
    
    CGRect rect = CGRectMake(0, 0, w,h );
    
    [self.composedButton addTarget:self action:@selector(composedButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    _composedButton.frame = CGRectOffset(rect, w *2, 0);
}


- (void)composedButtonClick{
    
    
    UIViewController *vc  =[[UIViewController alloc] init];
 
    if ([[UserAccountViewModel sharedUserAccount]userLogon ]) {
        
        ComposeViewController *Cvc =[[ComposeViewController alloc] init];
        vc =Cvc;
        
        
    }else{
    
        OAuthViewController *Ovc =[[OAuthViewController alloc] init];
        vc=Ovc;
    }

    
    UINavigationController  *Navc =[[UINavigationController alloc] initWithRootViewController:vc];
   
    [self presentViewController:Navc animated:YES completion:nil];
    
    NSLog(@"composedButtonClick");
    
}


-(UIButton *)composedButton {
    
    if (_composedButton==nil) {
        _composedButton =[[UIButton alloc] init];
        
        [self.tabBar addSubview:self.composedButton];
        
        
        [_composedButton
         setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        
        [_composedButton
         setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [_composedButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_composedButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
    }
    return _composedButton;
}

@end
